package com.ado.abyss.core.mybatisGenerator.code;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @@Author By Adonais
 * @description 代码生成器主函数
 **/
@Slf4j
public class GeneratorCodeMain {

	private static final Logger log = LoggerFactory.getLogger(GeneratorCodeMain.class) ;

	public static void main(String[] args) {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true ;    //如果已经生成过了是否进行覆盖
		String genCfg = "D:\\Dev_tools\\Java_ideaProject\\abyss\\abyss-dal\\src\\main\\java\\com\\ado\\abyss\\core\\mybatisGenerator\\file\\mybatisGeneratorConfig.xml";

		File configFile = new File(genCfg);
		ConfigurationParser cfgParser = new ConfigurationParser(warnings);//配置文件解析器
		Configuration config = null;
		try {
			config = cfgParser.parseConfiguration(configFile);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XMLParserException e) {
			e.printStackTrace();
		}
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator generator = null;
		try {
			generator = new MyBatisGenerator(config, callback, warnings);
		} catch (InvalidConfigurationException e) {
			e.printStackTrace();
		}
		try {
			generator.generate(null);
			if(warnings.isEmpty()) {
				log.info("建表成功"+"success!!!");
			}else {
				System.out.println("warnings :" + warnings);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
